import 'dart:async';

import 'package:flutter/services.dart';

final channel = const MethodChannel("CHANNEL");

/// 单次生效
/// 返回 Future
Future callNative<T>(String methodName, List<dynamic> args) async {
  try {
    return await channel.invokeMethod(methodName, args);
  } on PlatformException catch (error, trace) {}
}

/// 返回 Map
Future callNativeMap<K, V>(String methodName, List<dynamic> args) async {
  try {
    Map<K, V>? res = await channel.invokeMapMethod(methodName, args);
    return res;
  } on PlatformException catch (error, trace) {}
}

/// 返回 List
Future callNativeList<T>(String methodName, List<dynamic> args) async {
  try {
    List<T>? res = await channel.invokeListMethod(methodName, args);
    return res;
  } on PlatformException catch (error, trace) {}
}

/// 监听消息 处理原生的 send
Future dispatchNativeMethod(MethodCall call) async {
  print("Listen Native: ${call}");
}

/// 设置监听
/// PS: 不能在初始化 channel 时通过 .. 方式设置 会不生效
void initNativeChannel() {
  channel.setMethodCallHandler((MethodCall call) async {
    dispatchNativeMethod(call);
  });
}
